import {
  getDataByPage,
  jobDatasourceTest,
  jobDatasourceCreate,
  jobDatasourceUpdate,
  jobDatasourceDelete
} from '@/api/datasourcemanager';
import {
  Message
} from 'element-ui';

const sendWindowEvent = (name, data) => {
  const newEvent = new Event(name);
  newEvent.data = data;
  window.dispatchEvent(newEvent);
};

// 测试链接
const testConnect = (data) => {
  state.connectLoading = true;
  return jobDatasourceTest(data).then((res) => {
    setTimeout(() => {
      state.connectLoading = false;
    }, 1000);
    const result = dealRespponse(res);
    promptResponse(result, 'success');
    return Promise.resolve(res);
  });
};

// 创建
const createDataSource = data => {
  return jobDatasourceCreate(data).then(res => {
    promptResponse(res, 'success');
    if (res.code === 200 && res.success) {
      state.total = state.total + 1;
    }
    return Promise.resolve(res);
  });
};

// 修改
const upDataSource = data => {
  return jobDatasourceUpdate(data).then(res => {
    promptResponse(res, 'success');
    return Promise.resolve(res);
  });
};

const promptResponse = (res, type) => {
  const requestResult = res.success;
  if (type) {
    const map = {
      success: true,
      warning: false
    };
    for (const key in map) {
      if (key === type && requestResult === map[key]) {
        Message[type](res.message);
      }
    }
  }
  // 报false统一处理
  /*
  else {
    requestResult && Message.success(res.message);
    !requestResult && Message.warning(res.message);
  }
  */
};

// 删除
const delDataSource = ids => {
  const requestData = {
    idList: ids
  };
  return jobDatasourceDelete(requestData).then(res => {
    promptResponse(res);
    if (res.success) {
      state.total = state.total - 1;
    }
    return Promise.resolve(res);
  });
};

// 获取表体数据
const getTableData = () => {
  const {
    pageSize,
    name: datasourceName,
    group: datasourceGroup
  } = state;
  const requestData = {
    pageIndex: state.pageCurrent - 1,
    pageSize,
    datasourceName,
    datasourceGroup
  };
  return getDataByPage(requestData).then(res => {
    promptResponse(res, 'warning');
    return Promise.resolve(res);
  });
};

const dealRespponse = res => {
  if (res.success && res.code === 200) {
    return res;
  }
};

// 表体数据
const initTableData = () => {
  state.tableLoading = true;
  getTableData().then(res => {
    const tableData = dealRespponse(res);
    state.tableLoading = false;
    tableData && sendWindowEvent('DataSoruceTableData', tableData);
  });
};

// 不同key对应的事件
const todoListMap = Object.freeze({
  startSearch: initTableData,
  pageSize: initTableData,
  pageCurrent: initTableData
});

// 目前只能监听到第一层数据 多层需递归代理
const propObject = {
  name: '',
  group: '',
  pageCurrent: 1,
  pageSizes: [50, 100, 200, 400],
  pageSize: 50,
  total: 0,
  startSearch: false, // 仅仅用于抓获点击查询按钮事件
  connectLoading: false,
  tableLoading: false
};
const state = new Proxy(propObject, {
  set(target, key, value) {
    target[key] = value;
    if (key in todoListMap) {
      todoListMap[key](value);
    }
    return true;
  }
});

// 页面初始化
const initPage = () => {
  initTableData();
};
export {
  state,
  initPage,
  testConnect,
  createDataSource,
  upDataSource,
  delDataSource
};
